const bindEventKeydown = function(game) {
    let keydowns = {
        a: 'left',
        s: 'down',
        d: 'right',
        w: 'rotate',
        ArrowUp: 'rotate',
        ArrowDown: 'down',
        ArrowLeft: 'left',
        ArrowRight: 'right',
    }
    game.listeners.keydown = function(event) {
        let key = event.key
        let direction = keydowns[key]
        if (direction !== undefined) {
            if (direction === 'rotate') {
                move(game,direction)
            } else {
                game.move(direction)
            }
        }
    }
    window.addEventListener('keydown', game.listeners.keydown)
}
const bindEventKeyUp = function(game) {
    let keydowns = {
        a: 'left',
        s: 'down',
        d: 'right',
        ArrowDown: 'down',
        ArrowLeft: 'left',
        ArrowRight: 'right',
    }
    game.listeners.keyUp = function(event) {
        game.pressing = false
        let key = event.key
        let direction = keydowns[key]
        clearTimeout(game.actionTimers[direction])
        if (direction !== undefined) {
            game.buttonsTouch[direction] = false
        }
    }
    window.addEventListener('keyup', game.listeners.keyUp)
}
const removeEventKeyUp = function(game) {
    window.removeEventListener('keyup', game.listeners.keyUp)
}
const removeEventKeydown = function(game) {
    window.removeEventListener('keydown', game.listeners.keydown)
}
const bindEventLock = function(game) {
    game.listeners.lock = function(event) {
        let key = event.key
        if (key === ' ' && game.canLock) {
            lock(game)
            game.canLock = false
        }
    }
    window.addEventListener('keydown', game.listeners.lock)
}
const bindEventKeyupSpace = function(game) {
    window.addEventListener('keyup', event => {
        let key = event.key
        if (key === ' ') {
            game.canLock = true
        }
    })
}
const removeEventLock = function(game) {
    window.removeEventListener('keydown', game.listeners.lock)
}
const bindEventPause = function(game) {
    let container = e('#id-div-container')
    game.listeners.pause = function(event) {
        let playDiv = e('#id-div-play')
        let self = event.target
        if (self.id === 'id-div-pause') {
            clearTimeout(game.timer)
            clearTimeout(game.timer1)
            removeEventKeydown(game)
            removeEventKeyUp(game)
            removeEventLock(game)
            removeEventActions(game)
            playDiv.classList.add('show')
            self.classList.remove('show')
        }
    }
    container.addEventListener('click', game.listeners.pause)
}
const removeEventPause = function(game) {
    let container = e('#id-div-container')
    container.removeEventListener('click', game.listeners.pause)
}
const bindEventPlay = function(game) {
    let container = e('#id-div-container')
    let pauseDiv = e('#id-div-pause')
    container.addEventListener('click', (event) => {
        let self = event.target
        if (self.id === 'id-div-play') {
            game.run()
            loop(game, 'down')
            bindEventKeydown(game)
            bindEventKeyUp(game)
            bindEventLock(game)
            bindEventActions(game)
            pauseDiv.classList.add('show')
            self.classList.remove('show')
        }
    })
}
const bindEventRestart = function(game) {
    let container = e('#id-div-container')
    let dialog = e('.dialog')
    container.addEventListener('click', (event) => {
        let self = event.target
        if (self.id === 'id-div-restart') {
            let pauseDiv = e('#id-div-pause')
            if (pauseDiv.classList.contains('show')) {
                pauseDiv.click()
            }
            dialog.classList.add('show')
        }
    })
}
const bindEventConfirm = function(game) {
    let confirmButton = e('.yes')
    confirmButton.addEventListener('click', event => {
        let playDiv = e('#id-div-play')
        if (playDiv.classList.contains('show')) {
            playDiv.click()
        }
        restart(game)
        let dialog = e('.dialog')
        dialog.classList.remove('show')
    })
}
const bindEventCancel = function(game) {
    let cancelButton = e('.no')
    cancelButton.addEventListener('click', event => {
        let playDiv = e('#id-div-play')
        playDiv.click()
        let dialog = e('.dialog')
        dialog.classList.remove('show')
    })
}

const reset = function(game) {
    game.reset()
    let gameOverDiv = e('#id-div-gameover')
    gameOverDiv.classList.remove('show')
    removeEvents(game)

}
const restart = function(game) {
    reset(game)
    reload(game)
    loop(game, 'down')
    bindEventKeydown(game)
    bindEventKeyUp(game)
    bindEventPause(game)
    bindEventLock(game)
    bindEventActions(game)
}
const bindEventShowGuides = function() {
    let guidesDiv = e('.guides')
    guidesDiv.addEventListener('mouseover', () => {
        let operationsDiv = e('#id-div-operations')
        operationsDiv.classList.add('show')
    })
}
const bindEventHideGuides = function() {
    let guidesDiv = e('.guides')
    guidesDiv.addEventListener('mouseout', () => {
        let operationsDiv = e('#id-div-operations')
        operationsDiv.classList.remove('show')
    })
}
const bindEventActions = function(game) {
    let actions = e('#id-div-actions')
    game.listeners.actions = event => {
        let self = event.target
        if (self.classList.contains('left')) {
            game.moveLeft()
        } else if (self.classList.contains('right')) {
            game.moveRight()
        } else if (self.classList.contains('down')) {
            game.moveDown()
        } else if (self.classList.contains('rotate')) {
            move(game, 'rotate')
        } else if (self.classList.contains('lock')) {
            lock(game)
        }
    }
    actions.addEventListener('touchstart', game.listeners.actions)
}
const removeEventActions = function(game) {
    let actions = e('#id-div-actions')
    actions.removeEventListener('touchstart', game.listeners.actions)
}
const bindEventTouchEnd = function(game) {
    let actions = e('#id-div-actions')
    actions.addEventListener('touchend', event => {
        let self = event.target
        if (self.classList.contains('left')) {
            clearTimeout(game.actionTimers.left)
            game.buttonsTouch.left = false
        } else if (self.classList.contains('right')) {
            clearTimeout(game.actionTimers.right)
            game.buttonsTouch.right = false
        } else if (self.classList.contains('down')) {
            clearTimeout(game.actionTimers.down)
            game.buttonsTouch.down = false
        }
    })
}
const bindEvents = function(game) {
    bindEventPause(game)
    bindEventRestart(game)
    bindEventPlay(game)
    bindEventKeydown(game)
    bindEventLock(game)
    bindEventKeyupSpace(game)
    bindEventShowGuides()
    bindEventHideGuides()
    bindEventActions(game)
    bindEventTouchEnd(game)
    bindEventKeyUp(game)
    bindEventConfirm(game)
    bindEventCancel(game)
}
const removeEvents = function(game) {
    removeEventLock(game)
    removeEventKeydown(game)
    removeEventPause(game)
    removeEventActions(game)
}